<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_model extends DQ_Model 
{
    public function __construct()
    {
        parent::__construct();
    }

    public function check()
    {
        $captcha_error = $this->_checkLoginCaptcha();

        if($captcha_error)
        {
            return $captcha_error;
        }

        $checkedData = $this->checkInput(__FUNCTION__,0);

        $checkInfo = $this->_checkUser($checkedData['email'],$checkedData['password']);

        return $checkInfo;
    }

    private function _checkUser($email,$password)
    {
        //增加输入失败次数，成功后清零
        $login_count = (int) $this->session->userdata('login_count');
        $this->session->set_userdata('login_count', ++$login_count);

        $this->db->where('email',$email);
        $this->db->from('uc_member');
        $this->db->join('uc_member_detail', 'uc_member.uid = uc_member_detail.uid','left');

        $query = $this->db->get();

        $userInfo = $query->row_array();

        if(empty($userInfo))
        {
            return array('error'=>4001,'msg'=>$this->error_code[4001],'data'=>array('login_count'=>(int) $this->session->userdata('login_count')));
        }
        else
        {
            if(strcasecmp(strtoupper(md5(strtoupper(md5($password.$userInfo['salt'])))),$userInfo['password']) == 0)
            {
                $this->session->unset_userdata('login_count');
                $this->session->unset_userdata('ccode');

                $this->setLoginInfo($userInfo['uid'],$userInfo['username']);

                if($this->session->userdata('next'))
                {
                    $next = $this->session->userdata('next');
                    $this->session->unset_userdata('next');
                }
                else
                {
                    $next = '';
                }

                //临时 记住帐号
                //保存用户名cookie，否则删除
                if($this->input->post('remember'))
                {
                    setcookie("user",$email,time()+86400*30,'/');
                }
                else
                {
                    setcookie("user",'',time()-3600,'/');
                }

                return array('error'=>0,'msg'=>$this->error_code[0],'data'=>array('uid'=>$userInfo['uid'],'username'=>$userInfo['username'],'email'=>$userInfo['email'],'next'=>$next));
            }
            else
            {
                return array('error'=>4004,'msg'=>$this->error_code[4004],'data'=>array('login_count'=>(int) $this->session->userdata('login_count')));
            }
        }
    }

    public function setLoginInfo($uid,$username)
    {
        $this->session->set_userdata("uid", $uid);
        $this->session->set_userdata("username", $username);
    }

    private function _checkLoginCaptcha()
    {
        $login_count = $this->session->userdata('login_count');

        if(intval($login_count) < 3)
        {
            return 0;
        }
        else
        {
            return $this->_checkCaptcha();
        }
    }
}
